package backend.attribute.controller;


import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import backend.attribute.manager.AttributeManager;
import backend.attribute.manager.AttributeSetManager;
import backend.attribute.manager.AttributeValueManager;
import backend.paging.Paging;
import com.opensymphony.xwork2.ActionSupport;
import core.attribute.model.Attribute;
import core.attribute.model.AttributeSet;
import core.attribute.model.AttributeValue;


public class AttributeController extends ActionSupport implements ServletRequestAware
{
	private static final long serialVersionUID = 12323L;
	private AttributeValueManager avm ;
	private AttributeManager am ;
	private int pageSize ; // default value 10
	private int p ;
	private int totalPage ;
	private List<String> pageHtml ;
	private String msg;
	private String errorMsg ;
	private String backUrl ;
	private Long uuid ;
	private Attribute attribute ;
	private Set <AttributeValue> attributeValues ;
	private AttributeSet attributeSet ;
	private List<Attribute> attributes ;
	private List<String> values ;
	private Long valueUuid ;
	private String valuelabel ;
	private String newdropValue;
	private AttributeValue attributeValue ;
	private HttpServletRequest request;
	private String stringValue ;
	private String attributeType ;
	private Long attributeSetUuid ;
	private AttributeSetManager asm ;
	private Long attributeUuid ;
	
	public AttributeController()
	{
		am = new AttributeManager();
		avm = new AttributeValueManager();
		asm = new AttributeSetManager();
	}
	public String execute() 
	{
		List<Attribute> attrFullLst = am.getAttributeList();;
	
		if(this.pageSize == 0)
		{
			this.setPageSize(10);
		}
		Paging<Attribute> paging = new Paging<Attribute>();
		this.setPageHtml(paging.getPageHtml(this.getP(), pageSize, attrFullLst.size()));
		this.setAttributes(paging.getPageList(this.pageSize,attrFullLst , this.p));
		
		return SUCCESS ;
	}
	public String editAttribute()
	{
		this.setBackUrl("editAttributeSet?uuid="+this.getUuid()+"&p="+this.getP());
		this.setAttribute(am.getAttributeByUuid(this.getAttributeUuid()));
		this.setAttributeSet(asm.getAttributeSetByUuid(this.getUuid()));
		this.setAttributeValues(this.getAttribute().getValues());
		return SUCCESS ;
	}
	public String editValue()
	{
		this.setAttributeValue(avm.getAttributeValueByUuid(this.getValueUuid()));
		if(this.getAttributeValue().getStringValue()!=null)
		{
			this.setOptionsValues(this.getAttributeValue().getStringValue());
		}
		this.setAttribute(am.getAttributeByUuid(this.getAttributeUuid()));
		this.setAttributeSet(asm.getAttributeSetByUuid(this.getUuid()));
		this.setBackUrl("editAttribute?uuid="+this.getUuid()+"&p="+this.getP());
		return SUCCESS ;
	}
	public String changeValue()
	{
		//this.
		AttributeValue av = avm.getAttributeValueByUuid(this.getValueUuid());
		if(av.getStringValue()!= null)
		{
			this.setOptionsValues(av.getStringValue()); 
			Iterator <String> valItr = this.getValues().iterator();
			String newValue = new String("");
			while(valItr.hasNext())
			{
				String option = valItr.next().trim();
				if(request.getParameter(option) == null)
				{
					newValue = newValue + "#" + option ;
				}
			}
			av.setStringValue(newValue);
		}
		av.setLabel(valuelabel);
		avm.save(av);
		this.setAttributeValue(av);
		this.setOptionsValues(av.getStringValue());
		this.setAttribute(am.getAttributeByUuid(this.getAttributeUuid()));
		this.setAttributeSet(asm.getAttributeSetByUuid(this.getUuid()));
		this.setBackUrl("editAttribute?uuid="+this.getUuid()+"&p="+this.getP());
		return SUCCESS ;
	}
	public String changeSingleValue()
	{
		AttributeValue av = avm.getAttributeValueByUuid(this.getValueUuid());
		av.setLabel(this.getValuelabel());
		av.setStringValue(this.getStringValue());
		avm.save(av);
		this.setAttribute(am.getAttributeByUuid(this.getAttributeUuid()));
		this.setAttributeValues(this.getAttribute().getValues());
		this.setAttributeSet(asm.getAttributeSetByUuid(this.getUuid()));
		return SUCCESS ;
	}
	public String deleteAttributeValue()
	{
		AttributeValue av = avm.getAttributeValueByUuid(this.getValueUuid());
		if(av != null)
		{
			Attribute a = am.getAttributeByUuid(this.getAttributeUuid());
			Iterator<AttributeValue> avs = a.getValues().iterator();
			Set<AttributeValue> newAvs = new HashSet<AttributeValue>();
			while(avs.hasNext())
			{
				AttributeValue localAv = avs.next();
				if(!localAv.getUuid().equals(av.getUuid()))
				{
					newAvs.add(localAv);
				}
			}
			a.setValues(newAvs);
			am.save(a);
			avm.delete(av);
		}	
			this.setAttribute(am.getAttributeByUuid(this.getAttributeUuid()));
			this.setAttributeSet(asm.getAttributeSetByUuid(this.getUuid()));
			this.setAttributeValues(this.getAttribute().getValues());
		
		return SUCCESS ;
	}
	public String addNewValue()
	{
		AttributeValue av = avm.getAttributeValueByUuid(this.getValueUuid()) ;
		String newValue = this.getNewdropValue();
		String finalNew = new String("");
		if(av.getStringValue()==null)
		{
			finalNew = newValue;
		}
		else
		{
			finalNew = av.getStringValue() + "#" +newValue ;
		}
		av.setStringValue(finalNew);
		avm.save(av);
		this.setAttributeValue(av);
		this.setOptionsValues(this.getAttributeValue().getStringValue());
		this.setAttribute(am.getAttributeByUuid(this.getAttributeUuid()));
		this.setAttributeSet(asm.getAttributeSetByUuid(this.getUuid()));
		this.setBackUrl("editAttribute?uuid="+this.getUuid()+"&p="+this.getP());
		return SUCCESS ;
	}
	public String addAttributeValue()
	{
		this.setAttributeSet(asm.getAttributeSetByUuid(this.getUuid()));
		Attribute a = am.getAttributeByUuid(this.getAttributeUuid());
		AttributeValue av = new AttributeValue();
		av.setLabel(this.getValuelabel());
		av.setStringValue(this.getStringValue());
		av.setAttributeType(this.getAttributeType());
		//avm.save(av);
		Set<AttributeValue> avs = a.getValues();
		avs.add(av);
		a.setValues(avs);
		am.save(a);
		this.setAttribute(a);
		this.setAttributeValues(this.getAttribute().getValues());
		return SUCCESS ;
	}
	public void setOptionsValues(String value)
	{
		List <String> valLst = new ArrayList<String>(); 
		if(value != null)
		{
			
			StringTokenizer vls = new StringTokenizer(value , "#") ;
			while(vls.hasMoreTokens())
			{
				String s = vls.nextToken();
				valLst.add(s.trim());
			}
		}
		this.setValues(valLst);
	}

	public String createAttribute()
	{
		//am.save(this.getAttribute());
		AttributeSet as = asm.getAttributeSetByUuid(this.getAttributeSetUuid()) ;
		as.getAttributes().add(this.getAttribute());
		asm.saveAttributeSet(as);
		this.setAttributeSet(as);
		List<Attribute> atbL = new ArrayList<Attribute>();
		atbL.addAll(as.getAttributes());
		this.setAttributes(atbL);
		return SUCCESS ; 
	}
	public List<Attribute> getAttributes() {
		return attributes;
	}
	public void setAttributes(List<Attribute> attributes) {
		this.attributes = attributes;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getP() {
		return p;
	}
	public void setP(int p) {
		this.p = p;
	}
	public int getTotalPage() {
		return totalPage;
	}
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public String getErrorMsg() {
		return errorMsg;
	}
	public void setErrorMsg(String errorMsg) {
		this.errorMsg = errorMsg;
	}
	public List<String> getPageHtml() {
		return pageHtml;
	}
	public void setPageHtml(List<String> pageHtml) {
		this.pageHtml = pageHtml;
	}
	public String getBackUrl() {
		return backUrl;
	}
	public void setBackUrl(String backUrl) {
		this.backUrl = backUrl;
	}
	public Long getUuid() {
		return uuid;
	}
	public void setUuid(Long uuid) {
		this.uuid = uuid;
	}
	public Attribute getAttribute() {
		return attribute;
	}
	public void setAttribute(Attribute attribute) {
		this.attribute = attribute;
	}
	public Set<AttributeValue> getAttributeValues() {
		return attributeValues;
	}
	public void setAttributeValues(Set<AttributeValue> attributeValues) {
		this.attributeValues = attributeValues;
	}
	public AttributeSet getAttributeSet() {
		return attributeSet;
	}
	public void setAttributeSet(AttributeSet attributeSet) {
		this.attributeSet = attributeSet;
	}
	public List<String> getValues() {
		return values;
	}
	public void setValues(List<String> values) {
		this.values = values;
	}
	public Long getValueUuid() {
		return valueUuid;
	}
	public void setValueUuid(Long valueUuid) {
		this.valueUuid = valueUuid;
	}
	public String getValuelabel() {
		return valuelabel;
	}
	public void setValuelabel(String valuelabel) {
		this.valuelabel = valuelabel;
	}
	public String getNewdropValue() {
		return newdropValue;
	}
	public void setNewdropValue(String newdropValue) {
		this.newdropValue = newdropValue;
	}
	public AttributeValue getAttributeValue() {
		return attributeValue;
	}
	public void setAttributeValue(AttributeValue attributeValue) {
		this.attributeValue = attributeValue;
	}
	@Override
	public void setServletRequest(HttpServletRequest req) {
		// TODO Auto-generated method stub
		this.request = req ;
	}
	public String getStringValue() {
		return stringValue;
	}
	public void setStringValue(String stringValue) {
		this.stringValue = stringValue;
	}
	public String getAttributeType() {
		return attributeType;
	}
	public void setAttributeType(String attributeType) {
		this.attributeType = attributeType;
	}
	public Long getAttributeSetUuid() {
		return attributeSetUuid;
	}
	public void setAttributeSetUuid(Long attributeSetUuid) {
		this.attributeSetUuid = attributeSetUuid;
	}
	public Long getAttributeUuid() {
		return attributeUuid;
	}
	public void setAttributeUuid(Long attributeUuid) {
		this.attributeUuid = attributeUuid;
	}
	
}
